"""
@file:   email
@author: FrankYi
@date:   2020/08/15
@desc:
"""

"""
@file:   user
@author: FrankYi
@date:   2020/08/12
@desc:
"""

from libs.bp import NestableBlueprint
from flask_restful import Api, Resource
from flask import request,g
from mail.sendmail import send
import random
from libs.response import genrate_response
from forms.login import LoginForm
from libs.statuscode import FormValidateException
from libs.exceptionhandle import default_error_handler

tools_bp = NestableBlueprint('tools', __name__, url_prefix='/tools')
api = Api(tools_bp)

api.handle_error = default_error_handler

class SendEmail(Resource):
    def post(self):
        data = request.json
        form = LoginForm(data=data)
        if form.validate():
            rece_email = data.get('email').split(' ')
            auth = random.randint(1000, 9999)
            msg = f'''感谢您对South的大力支持!
            此次验证码为{auth}'''
            send(rece_email, '验证码', msg)
            return genrate_response(data=str(auth),message='发送成功!', status_code=102)
        else:
            result = form.errors
            raise FormValidateException(message=result)

api.add_resource(SendEmail, '/email/')